﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace SFast
{
    public static class MyJs
    {
        #region alert()  弹出信息提示不跳转
        /// <summary>
        /// 弹出信息提示 不跳转，当前页面刷新
        /// </summary>
        /// <param name="message">信息</param>
        public static void Alert(String message)
        {
            Alert(message, "location.href");
        }
        #endregion

        #region Alert() 弹出信息提示 并跳转
        /// <summary>
        /// 弹出信息提示 并跳转
        /// </summary>
        /// <param name="Message">需要弹出的信息</param>
        /// <param name="Url">需要跳转的地址</param>
        public static void Alert(string Message, string Url)
        {
            Alert("myjs", Message, Url);
        }
        #endregion

        #region Alert() 弹出信息提示 并跳转地址
        /// <summary>
        /// 弹出信息提示 并跳转地址
        /// </summary>
        /// <param name="message">信息</param>
        /// <param name="url">地址可以加入~/</param>
        public static void Alert(string Key, String message, String url)
        {
            String js = GetAlertStr(message, url);
            if (!IsAjax())
            {
                HttpContext.Current.Response.Write(js);
                HttpContext.Current.Response.End();
            }
            else
            {
                var page = HttpContext.Current.CurrentHandler as Page;
                System.Web.UI.ScriptManager.RegisterStartupScript(page, typeof(Page), "MyJs_" + Key, js, false);
            }

        }
        #endregion

        #region GetAlertStr() 获取弹提示信息的js脚本字符串
        /// <summary>
        /// 获取弹提示信息的js脚本字符串
        /// </summary>
        /// <param name="message">要弹出的信息</param>
        /// <param name="url">最后跳转的页面地址</param>
        /// <returns></returns>
        public static String GetAlertStr(String message, String url)
        {
            string urlPath, urlPar;  //url地址(?号之前)  url参数(?号之后)
            if (url.IndexOf("~/") != -1)
            {
                urlPath = url.IndexOf("?") != -1 ? url.Split('?')[0] : url;   //?号前url
                urlPar = url.Split('?').Length > 1 ? url.Split('?')[1] : ""; //？后参数
                url = VirtualPathUtility.ToAbsolute(urlPath) + "?" + urlPar;
            }
            url = url == "location.href" ? "location.href" : "'" + url + "'";

            return "<script language='JavaScript' type='text/javascript'> alert('" + message + "');location.href=" + url + ";</script>";
        }
        #endregion

        #region GetRedirectJsStr() 获取跳转页面的js字符串
        /// <summary>
        /// 获取跳转页面的js字符串
        /// </summary>
        /// <param name="url">新url地址</param>
        /// <returns></returns>
        public static String GetRedirectJsStr(String url)
        {
            string urlPath, urlPar;  //url地址(?号之前)  url参数(?号之后)
            if (url.IndexOf("~/") != -1)
            {
                urlPath = url.IndexOf("?") != -1 ? url.Split('?')[0] : url;   //?号前url
                urlPar = url.Split('?').Length > 1 ? url.Split('?')[1] : ""; //？后参数
                url = VirtualPathUtility.ToAbsolute(urlPath) + "?" + urlPar;
            }
            url = url == "location.href" ? "location.href" : "'" + url + "'";
            return "<script language='JavaScript' type='text/javascript'> location.href=" + url + ";</script>";
        }
        #endregion

        #region GetJavascript 把js代码两端加入script 标签
        /// <summary>
        /// 把js代码两端加入script 标签
        /// </summary>
        /// <param name="jscode"></param>
        /// <returns></returns>
        public static String GetJavascript(String jscode)
        {
            return "<script language='JavaScript' type='text/javascript'> " + jscode + "</script>";
        }
        #endregion

        #region GetFormJs 获取把对象回添到表单中的js
        /// <summary>
        /// 获取把对象回添到表单中的js
        /// </summary>
        /// <param name="formName">表单名称</param>
        /// <param name="obj">数据对象</param>
        /// <param name="otherCode">其它代码</param>
        /// <param name="map">如果对象字段名称不和表单中控件name相同时可以用此映射一下。格式 ： name : name ,name1:name2</param>
        /// <returns></returns>
        public static String GetFormJs(String formName, object obj, String otherCode = "", object map = null)
        {
            String js = String.Empty;
            if (map != null)
            {
                js = "<script type=\"text/javascript\">myform({ formid: '" + formName + "', data: " + obj._ToJsonStr() + " ,map:" + map._ToJsonStr() + " }); " + otherCode + "</script>";
            }
            else
            {
                js = "<script type=\"text/javascript\">myform({ formid: '" + formName + "', data: " + obj._ToJsonStr() + " }); " + otherCode + "</script>";
            }
            return js;
        }
        #endregion

        #region ScriptManager 向页面注册JS脚本
        /// <summary>
        /// 向页面注册JS脚本
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="js">脚本文件</param>
        /// <param name="regtp">脚本注册位置,1[Start],0[Block]</param>
        public static void ScriptManager(Page page, string JavaScript, int regtp, string regkey)
        {
            StringBuilder strscript = new StringBuilder();
            ClientScriptManager cs = page.ClientScript;
            if (regtp == 1)
            {
                if (!cs.IsStartupScriptRegistered(regkey))
                {
                    cs.RegisterStartupScript(page.GetType(), regkey, JavaScript);
                }
            }
            else
            {
                if (!cs.IsClientScriptBlockRegistered(regkey))
                {
                    cs.RegisterClientScriptBlock(page.GetType(), regkey, JavaScript);
                }
            }

        }
        #endregion

        #region IsAjax 判断此请求是否为ajax请求
        /// <summary>
        /// 判断此请求是否为ajax请求
        /// </summary>
        /// <returns></returns>
        public static bool IsAjax()
        {
            return System.Web.UI.ScriptManager.GetCurrent(HttpContext.Current.CurrentHandler as Page) != null;
        }
        #endregion
    }
}
